﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Model
{

    public class Expedient
    {
        public int IDEx { get; set; }
        public string NroExped { get; set; }
        public int IDTIP { get; set; }
        public int IDMat { get; set; }
        public int IDSMat { get; set; }
        public int IDLoc { get; set; }
        public int IDEstado { get; set; }
        public DateTime FechaRegistro { get; set; }
        public DateTime FechaExped { get; set; }
        public int PerRec { get; set; }
        public string Sumilla { get; set; }
        public string PDemanda { get; set; }
        public bool EstDem { get; set; }
        public double MontoS { get; set; }
        public double MontoUS { get; set; }
        public string Juez { get; set; }
        public int Abogado { get; set; }
        public int idUbicacion { get; set; }
        public string DesUbica { get; set; }
        public DateTime FechaVmto { get; set; }
        public bool Vence { get; set; }

        public Expedient()
        {
            IDEx = 0;
            NroExped = "";
            IDTIP = 1;
            IDMat  = 1;
            IDSMat  = 1;
            IDLoc  = 1;
            IDEstado  = 1;
            FechaRegistro = DateTime.Now;
            FechaExped = DateTime.Now;
            PerRec = 0;
            Sumilla  = "";
            PDemanda  = "";
            EstDem  = false;
            MontoS  = 0;
            MontoUS  = 0;
            Juez  = "";
            Abogado  = 0;
            idUbicacion  = 1;
            DesUbica = "";
            FechaVmto = DateTime.Now;
            Vence  = false;
        }

        public bool Save() 
        {
            string sql = @"INSERT INTO [LEG_Expedientes]
                               ([NroExped]"+
                               ",[IDTIP]"+
                               ",[IDMat]"+
                               ",[IDSMat]"+
                               ",[IDLoc]"+
                               ",[IDEstado]"+
                               ",[FechaRegistro]"+
                               ",[FechaExped]"+
                               ",[PerRec]"+
                               ",[Sumilla]"+
                               ",[PDemanda]"+
                               ",[EstDem]"+
                               ",[MontoS]"+
                               ",[MontoUS]"+
                               ",[Juez]"+
                               ",[Abogado]"+
                               ",[idUbicacion]"+
                               ",[DesUbica]"+
                               ",[FechaVmto]"+
                               ",[Vence]"+
                               " )"+
                         "VALUES"+
                               "(@NroExped" +
                               ",@IDTIP" +
                               ",@IDMat" +
                               ",@IDSMat" +
                               ",@IDLoc" +
                               ",@IDEstado" +
                               ",@FechaRegistro" +
                               ",@FechaExped" +
                               ",@PerRec" +
                               ",@Sumilla" +
                               ",@PDemanda" +
                               ",@EstDem" +
                               ",@MontoS" +
                               ",@MontoUS" +
                               ",@Juez" +
                               ",@Abogado" +
                               ",@idUbicacion" +
                               ",@DesUbica" +
                               ",@FechaVmto" +
                               ",@Vence" +
                        ")";

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Utilitarios.ConnKLegal;

            try
            {
                
                conn.Open();                
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.Add("@NroExped", SqlDbType.NVarChar, 20).Value = this.NroExped;
                cmd.Parameters.Add("@IDTIP", SqlDbType.Int).Value = this.IDTIP;
                cmd.Parameters.Add("@IDMat", SqlDbType.Int).Value = this.IDMat;
                cmd.Parameters.Add("@IDSMat", SqlDbType.Int).Value = this.IDSMat;
                cmd.Parameters.Add("@IDLoc", SqlDbType.Int).Value = this.IDLoc;
                cmd.Parameters.Add("@IDEstado", SqlDbType.Int).Value = this.IDEstado;
                cmd.Parameters.Add("@FechaRegistro", SqlDbType.DateTime).Value = this.FechaRegistro;
                cmd.Parameters.Add("@FechaExped" , SqlDbType.DateTime).Value = this.FechaExped;
                cmd.Parameters.Add("@PerRec", SqlDbType.Int).Value = this.PerRec;
                cmd.Parameters.Add("@Sumilla", SqlDbType.NVarChar, 500).Value = this.Sumilla;
                cmd.Parameters.Add("@PDemanda", SqlDbType.NVarChar, 200).Value = this.PDemanda;
                cmd.Parameters.Add("@EstDem", SqlDbType.Bit).Value = this.EstDem;
                cmd.Parameters.Add("@MontoS", SqlDbType.Decimal).Value = this.MontoS;
                cmd.Parameters.Add("@MontoUS", SqlDbType.Decimal).Value = this.MontoUS;
                cmd.Parameters.Add("@Juez" , SqlDbType.NVarChar, 50).Value = this.Juez;
                cmd.Parameters.Add("@Abogado", SqlDbType.Int).Value = this.Abogado;
                cmd.Parameters.Add("@idUbicacion" , SqlDbType.Int).Value = this.idUbicacion;
                cmd.Parameters.Add("@DesUbica", SqlDbType.NVarChar, 20).Value = this.DesUbica;
                cmd.Parameters.Add("@FechaVmto" , SqlDbType.DateTime).Value = this.FechaVmto;
                cmd.Parameters.Add("@Vence", SqlDbType.Bit).Value = this.Vence;
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {   
                if (conn != null)
                {
                    conn.Close();                    
                }
            }
            return true;            
          
        }
    }

}
